package com.gxa.weixing.pc.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.weixing.core.common.JsonResult;
import com.gxa.weixing.core.exception.BusinessException;
import com.gxa.weixing.pc.service.GoodsService;
import com.gxa.weixing.pc.service.ShopInventoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2022/01/06  14:17:07
 */
@RestController
@RequestMapping("/shopInventory")
@Api(tags = "门店库存管理Api")
public class ShopInventoryController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private ShopInventoryService shopInventoryService;

    /**
     * 根据特定条件获取门店库存列表的接口
     *
     * @param map 条件
     *            name: 商品名称 模糊
     *            state:销售状态
     *            shopGoodsState:销售状态
     *            shopName:门店名称
     *            page: 页码
     *            size: 每页数据量
     * @return 返回信息
     */
    @PostMapping("/getShopGoodsList")
    @ApiOperation("根据特定条件获取门店库存列表的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body")
    public Map<String, Object> getShopGoodsList(@RequestBody Map<String, Object> map) throws BusinessException, ParseException {
        // 获取查询的数据
        PageInfo<Map<String, Object>> shopGoodsList = goodsService.getShopGoodsList(map);
        // 给前端返回消息
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(shopGoodsList.getList());
        jsonResult.put("count", shopGoodsList.getTotal());
        return jsonResult.getDataMap();
    }


    /**
     * 修改门店库存的接口
     *
     * @param map 修改休息
     * @return 返回是否成功
     */
    @PostMapping("/update")
    @ApiOperation("修改门店库存的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body", dataType = "Map")
    public Map<String, Object> update(@RequestBody Map<String, Object> map) throws BusinessException {
        shopInventoryService.updateShopInventory(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }


    /**
     * 删除门店库存的接口
     *
     * @param id 删除门店库存的编号
     * @return 返回是否成功
     */
    @PostMapping("/delete")
    @ApiOperation("删除门店库存的接口")
    @ApiImplicitParam(name = "id", value = "编号", paramType = "query")
    public Map<String, Object> delete(Long id) throws BusinessException {
        shopInventoryService.deleteShopInventory(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }


    /**
     * 添加门店库存的接口
     *
     * @param map 添加门店库存的信息
     * @return 返回是否成功
     */
    @PostMapping("/add")
    @ApiOperation("添加门店库存的接口")
    @ApiImplicitParam(name = "map", value = "map", paramType = "body", dataType = "Map")
    public Map<String, Object> add(@RequestBody Map<String, Object> map) throws BusinessException {
        shopInventoryService.addShopInventory(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }


}
